home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / help.c < prev    next >
Text File  |  1994-04-15  |  8KB  |  289 lines

  1. /**********************************************************************\
  2.  
  3. File:        help.c
  4.  
  5. Purpose:    This module handles displaying the different help windows.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "graphics.h"            /* must come first because it defines WindowDataHandle */
  25. #include "help.h"
  26. #include "menus.h"
  27. #include "util.h"
  28. #include "program globals.h"
  29.  
  30. int                gNumHelp;
  31. int                gWhichHelp;
  32. int                gLastHelp;
  33.  
  34. /*-----------------------------------------------------------------------------------*/
  35. /* internal stuff for help.c                                                         */
  36.  
  37. void SetupTheHelpWindow(WindowDataHandle theData);
  38. void ShutdownTheHelpWindow(WindowDataHandle theData);
  39. void InitializeTheHelpWindow(WindowDataHandle theData);
  40. void OpenTheHelpWindow(WindowDataHandle theData);
  41. void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed);
  42. void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc);
  43. void DrawTheHelp(void);
  44. void AddNameToString(Str255 theLine, Str255 theStringToAdd);
  45.  
  46. static    Handle            gHelpIcon[4];
  47.  
  48.  
  49. int HelpWindowDispatch(ExtendedWindowDataHandle theData, int theMessage, unsigned long misc)
  50. {
  51.     unsigned char    theChar;
  52.     Point            thePoint;
  53.     
  54.     switch (theMessage)
  55.     {
  56.         case kUpdate:
  57.             DrawTheHelp();
  58.             return kSuccess;
  59.             break;
  60.         case kKeydown:
  61.             theChar=misc&charCodeMask;
  62.             KeyPressedInHelpWindow(theData, theChar);
  63.             return kSuccess;
  64.             break;
  65.         case kMousedown:
  66.             thePoint.h=(misc>>16)&0x7fff;
  67.             thePoint.v=misc&0x7fff;
  68.             MouseClickedInHelpWindow(theData, thePoint);
  69.             return kSuccess;
  70.             break;
  71.         case kOpen:
  72.             OpenTheHelpWindow(theData);
  73.             return kSuccess;
  74.             break;
  75.         case kInitialize:
  76.             InitializeTheHelpWindow(theData);
  77.             return kSuccess;
  78.             break;
  79.         case kStartup:
  80.             SetupTheHelpWindow(theData);
  81.             return kSuccess;
  82.             break;
  83.         case kShutdown:
  84.             ShutdownTheHelpWindow(theData);
  85.             return kSuccess;
  86.             break;
  87.     }
  88.     
  89.     return kFailure;        /* revert to default processing for all other messages */
  90. }
  91.  
  92. void SetupTheHelpWindow(WindowDataHandle theData)
  93. {
  94.     int                i;
  95.     unsigned char    *helpStr="\pHelp";
  96.     
  97.     for (i=0; i<4; i++)
  98.         gHelpIcon[i]=GetIcon(128+i);        /* get icons from .rsrc file */
  99.     gNumHelp=CountMItems(gHelpMenu);        /* total # of help screens */
  100.     gLastHelp=0;                            /* # of last selected help screen */
  101.     
  102.     (**theData).windowWidth=300;
  103.     (**theData).windowHeight=250;
  104.     (**theData).windowType=noGrowDocProc;    /* document-looking thing */
  105.     (**theData).hasCloseBox=TRUE;
  106.     (**theData).windowBounds.top=50;
  107.     (**theData).windowBounds.left=10;
  108.     Mymemcpy((**(gTheWindowData[kHelp])).windowTitle, helpStr, helpStr[0]+1);
  109. }
  110.  
  111. void ShutdownTheHelpWindow(WindowDataHandle theData)
  112. {
  113.     int            i;
  114.     
  115.     for (i=0; i<4; i++)
  116.         ReleaseResource(gHelpIcon[i]);            /* clean up by disposing icons */
  117. }
  118.  
  119. void InitializeTheHelpWindow(WindowDataHandle theData)
  120. {
  121.     (**theData).initialTopLeft.v=(**theData).windowBounds.top-9;
  122.     (**theData).initialTopLeft.h=(**theData).windowBounds.left;
  123. }
  124.  
  125. void OpenTheHelpWindow(WindowDataHandle theData)
  126. {
  127.     if (gWhichHelp!=gLastHelp)                    /* if new help screen, force update */
  128.         (**theData).offscreenNeedsUpdate=TRUE;    /* of offscreen GWorld/bitmap */
  129. }
  130.  
  131. void KeyPressedInHelpWindow(WindowDataHandle theData, unsigned char keyPressed)
  132. {
  133.     switch (keyPressed)
  134.     {
  135.         case 0x1c:                                        /* left arrow */
  136.             gLastHelp=gWhichHelp;                        /* save last screen # */
  137.             gWhichHelp--;                                /* decrement screen # */
  138.             if (gWhichHelp==0)                            /* wrap around */
  139.                 gWhichHelp=gNumHelp;
  140.             OpenTheWindow((**theData).windowIndex);        /* display new screen */
  141.             break;
  142.         case 0x1d:                                        /* right arrow */
  143.             gLastHelp=gWhichHelp;                        /* save last screen # */
  144.             gWhichHelp++;                                /* increment screen # */
  145.             if (gWhichHelp>gNumHelp)                    /* wraparound */
  146.                 gWhichHelp=1;
  147.             OpenTheWindow((**theData).windowIndex);        /* display new screen */
  148.             break;
  149.     }
  150. }
  151.  
  152. void MouseClickedInHelpWindow(WindowDataHandle theData, Point mouseLoc)
  153. {
  154.     Rect        iconRect;
  155.     Boolean        isHilited;
  156.     
  157.     SetRect(&iconRect, 20, 7, 52, 39);        /* rectangle where left arrow icon is */
  158.     if (PtInRect(mouseLoc, &iconRect))        /* if we hit it... */
  159.     {
  160.         isHilited=FALSE;
  161.         while (StillDown())                    /* track mouse */
  162.         {
  163.             GetMouse(&mouseLoc);            /* returns point in window's local coords */
  164.             if (PtInRect(mouseLoc, &iconRect))    /* if still in icon's rectangle */
  165.             {
  166.                 if (!isHilited)                /* hilight icon if not already hilighted */
  167.                     PlotIcon(&iconRect, gHelpIcon[2]);
  168.                 isHilited=TRUE;                /* so we know */
  169.             }
  170.             else                            /* else we moved outside icon's rectangle */
  171.             {
  172.                 if (isHilited)                /* dehilight if highlighted */
  173.                     PlotIcon(&iconRect, gHelpIcon[0]);
  174.                 isHilited=FALSE;            /* so we know */
  175.             }
  176.         }
  177.         if (isHilited)                        /* if we were still in icon's rect on mouseup */
  178.         {
  179.             gLastHelp=gWhichHelp;            /* save last help screen # */
  180.             gWhichHelp--;                    /* decrement screen # */
  181.             if (gWhichHelp==0)                /* wrap around */
  182.                 gWhichHelp=gNumHelp;
  183.             OpenTheWindow((**theData).windowIndex);        /* display new help screen */
  184.         }
  185.     }
  186.     else    /* do same thing as above, only with the right arrow icon */
  187.     {
  188.         OffsetRect(&iconRect, 228, 0);
  189.         if (PtInRect(mouseLoc, &iconRect))
  190.         {
  191.             isHilited=FALSE;
  192.             while (StillDown())
  193.             {
  194.                 GetMouse(&mouseLoc);
  195.                 if (PtInRect(mouseLoc, &iconRect))
  196.                 {
  197.                     if (!isHilited)
  198.                         PlotIcon(&iconRect, gHelpIcon[3]);
  199.                     isHilited=TRUE;
  200.                 }
  201.                 else
  202.                 {
  203.                     if (isHilited)
  204.                         PlotIcon(&iconRect, gHelpIcon[1]);
  205.                     isHilited=FALSE;
  206.                 }
  207.             }
  208.             if (isHilited)
  209.             {
  210.                 gLastHelp=gWhichHelp;
  211.                 gWhichHelp++;
  212.                 if (gWhichHelp>gNumHelp)
  213.                     gWhichHelp=1;
  214.                 OpenTheWindow((**theData).windowIndex);
  215.             }
  216.         }
  217.     }
  218. }
  219.  
  220. void DrawTheHelp(void)
  221. {
  222.     GrafPtr            curPort;
  223.     int                row;
  224.     Handle            textHandle;
  225.     Str255            theLine;
  226.     unsigned long    pos;
  227.     unsigned long    theSize;
  228.     unsigned char    theChar;
  229.     Rect            iconRect;
  230.     int                theWidth;
  231.     
  232.     GetPort(&curPort);
  233.     EraseRect(&(curPort->portRect));
  234.     
  235.     theWidth=curPort->portRect.right-curPort->portRect.left;
  236.     
  237.     TextFont(geneva);
  238.     TextSize(9);
  239.     row=63;
  240.     textHandle=GetResource('TEXT', 399+gWhichHelp);        /* get text from .rsrc file */
  241.     if (textHandle==0L)        /* if not there, abort */
  242.         return;
  243.     if (*textHandle==0L)    /* if there but SetResLoad=FALSE, load it already! */
  244.         LoadResource(textHandle);
  245.     if (*textHandle==0L)    /* if still no luck, abort */
  246.         return;
  247.     pos=0L;
  248.     theSize=SizeResource(textHandle);    /* size of text */
  249.     while (pos<theSize)
  250.     {
  251.         theLine[0]=0x00;
  252.         /* the beauty of C -- this just gathers characters in theLine (pascal string)
  253.            until (1) the end of the text, or (2) a return character */
  254.         while ((pos<theSize) && ((theChar=*((unsigned char*)(((long)(*textHandle))+(pos++))))!=0x0d))
  255.         {
  256.             if (theChar=='^')        /* ^ character means insert program name */
  257.                 AddNameToString(theLine, APPLICATION_NAME);
  258.             else theLine[++theLine[0]]=theChar;
  259.         }
  260.         MoveTo(8, row);
  261.         DrawString(theLine);        /* draw one line of help */
  262.         row+=12;
  263.     }
  264.     
  265.     ReleaseResource(textHandle);    /* important!  or we'll get memory leaks */
  266.     
  267.     MoveTo(0, 46);
  268.     Line(300, 0);
  269.     
  270.     SetRect(&iconRect, 20, 7, 52, 39);
  271.     PlotIcon(&iconRect, gHelpIcon[0]);        /* plot left arrow icon */
  272.     OffsetRect(&iconRect, 228, 0);
  273.     PlotIcon(&iconRect, gHelpIcon[1]);        /* plot right arrow icon */
  274.     
  275.     GetItem(gHelpMenu, gWhichHelp, theLine);    /* get help screen title from help menu */
  276.     TextFace(bold);
  277.     MoveTo((theWidth-StringWidth(theLine))/2, 26);        /* center it */
  278.     DrawString(theLine);
  279.     TextFace(0);
  280. }
  281.  
  282. void AddNameToString(Str255 theLine, Str255 theStringToAdd)
  283. {
  284.     int            i;
  285.     
  286.     for (i=1; i<=theStringToAdd[0]; i++)    /* insert application name into help string */
  287.         theLine[++theLine[0]]=theStringToAdd[i];
  288. }
  289.